<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2019/5/7
 * Time: 下午5:49
 */

namespace App\Http\Controllers;

use App\Http\Model\FinanceCharm;
use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\RoomMsgServices;
use App\Http\Services\RoomTallyServices;

class RoomTallyController extends BaseController
{
    private $serviceRoomTally;
    private $serviceRoomMsg;


    public function __construct()
    {
        $this->serviceRoomTally = new RoomTallyServices();
        $this->serviceRoomMsg = new RoomMsgServices();
    }


    /**
     * 开始
     */
    public function open()
    {
        do {
            if (!$this->checkRequest()) {
                break;
            }


            if ($this->requestData['room_id']) {
                $data = $this->serviceRoomTally->info($this->requestData['room_id']);
                $tally_data = $data['data']['tally_data'];
                foreach ($tally_data as $tally) {
                    $model = new FinanceCharm();
//                write_log('Liyu',$tally['user_id'],[]);
                    $model->clearCharmLog($tally['user_id'], $this->requestData['room_id']);
                }
            }


            $this->callCommon($this->serviceRoomTally, 'open', $this->requestData, false);
            $roomId = $this->requestData['room_id'] ?? null;
            $opened = $this->responseData['data']['opened'] ?? null;
            if (is_null($roomId) || is_null($opened)) {
                break;
            }
            $this->serviceRoomMsg->broadcastTallyState($roomId, $opened);
        } while (false);

        if (is_string($this->responseData))
            $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }


    /**
     * 关闭
     */
    public function close()
    {
        do {
            if (!$this->checkRequest()) {
                break;
            }

            $this->callCommon($this->serviceRoomTally, 'close', $this->requestData, false);
            $roomId = $this->requestData['room_id'] ?? null;
            $opened = $this->responseData['data']['opened'] ?? null;
            if (is_null($roomId) || is_null($opened)) {
                break;
            }
            $this->serviceRoomMsg->broadcastTallyState($roomId, $opened);

        } while (false);

        if (is_string($this->responseData))
            $this->responseData = json_decode($this->responseData, true);
        Response::returnResult($this->responseData['code'], $this->responseData['data'], $this->responseData['msg']);
    }

    public function getCharmLogList(Request $request)
    {
        $userId = $request->input('user_id');
        // write_log("finance", "zq-get-charm-log", [$userId]);
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input('room_id');
        // write_log("finance", "zq-get-charm-log", [$roomId]);
        if (empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $model = new FinanceCharm();
        // write_log("finance", "zq-get-charm-log", ['ok']);
        $pageSize = $request->input('pageSize', 10);
        $page = $request->input('page');
        $list = $model->getCharmLogList($userId, $roomId, $pageSize, $page);
        $list = objToArray($list);
        Response::returnResult(Hint::SUCCESS, $list);
    }

    /**
     * 信息
     */
    public function info(Request $request)
    {
        $userId = $this->getLoginUserId();
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input('room_id');
        if (empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->serviceRoomTally->info($roomId);
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 重制房间指定用户计数
     */
    public function resetUserTally(Request $request)
    {
        $userId = $this->getLoginUserId();
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $roomId = $request->input('room_id');
        $toUserIds = $request->input('to_user_ids');
        if (empty($roomId) || empty($toUserIds)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $toUserIds = explode(',', $toUserIds);
        $toUserIds = array_filter(array_unique($toUserIds));

        $data = $this->serviceRoomTally->resetUserTally($roomId, $userId, $toUserIds);

        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

    /**
     * 更改房间真爱榜状态
     */
    public function changeLoveRank(Request $request)
    {
        $roomId = $request->input('room_id', 0);
        $userId = $this->getLoginUserId();
        if (empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $status = $request->input('status', 0);
        if (empty($roomId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $data = $this->serviceRoomTally->changeLoveRank($roomId, $userId, $status);
        Response::returnResult($data['code'], $data['data'], $data['msg']);
    }

}